package com.minarto.display.graphics {
	import com.minarto.utils.MathX;
	import flash.display.Graphics;
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author minarto
	 */
	public class GraphicsEx 
	{
		static public function coolTimeRect($graphics:Graphics, $x:Number, $y:Number, $width:Number, $height:Number, $rate:Number = 0):void {
			var w:Number, h:Number, ra:Number, x:Number, y:Number, cx:Number, cy:Number, r:Number,
				f:Function, p:Point;
			
			if (!$graphics || !$rate) {
				return;
			}
			else if (Math.abs($rate) < 1) {
				w = $width * .5;
				h = $height * .5;
				
				p = MathX.POINT;
				p.x = w;
				p.y = h;
				r = p.length;
				
				cx = $x + w;
				cy = $y + h;
				$graphics.moveTo(cx, $y);
				
				f = $graphics.lineTo;
				
				
				f(cx, cy);
				
				ra = ($rate - .25) * 360 * MathX.TO_RADIAN;
				x = Math.cos(ra) * r;
				if (x < - w) {
					f($x, cy - Math.tan(ra) * w);
					f($x, $y);
				}
				else if (x > w) {
					h = $y + $height;
					f($x + $width, cy + Math.tan(ra) * w);
					f($x + $width, h);
					f($x, h);
					f($x, $y);
				}
				else {
					y = Math.sin(ra) * r;
					if (x < 0) {
						if (y < 0) {
							f(cx - h / Math.tan(ra), $y);
						}
						else {
							h = $y + $height;
							f(cx + h / Math.tan(ra), h);
							f($x, h);
							f($x, $y);
						}
					}
					else {
						if (y < 0) {
							f(cx, cy);
							f(cx - h / Math.tan(ra), $y);
							w = $x + $width;
							f(w, $y);
							f(w, $y + $height);
						}
						else {
							f(cx + h / Math.tan(ra), $y + $height);
						}
						f($x, $y + $height);
						f($x, $y);
					}
				}
				f(cx, $y);
			}
			else {
				$graphics.drawRect($x, $y, $width, $height);
			}
		}
		
		
		static public function coolTimeEllipse($graphics:Graphics, $x:Number, $y:Number, $radius:Number, $yradius:Number, $rate:Number = 0):void {
			var cos:Function, sin:Function, drawTo:Function,
				c:int,
				ra:Number, r:Number, x:Number, y:Number, px:Number, py:Number, r2:Number, yr2:Number, t0:Number, t1:Number;
			
			if (!$graphics || !$rate) {
				return;
			}
			else if (Math.abs($rate) < 1) {
				$graphics.moveTo($x, $y - $yradius);
				
				drawTo = $graphics.lineTo;
				drawTo($x, $y);
				
				cos = Math.cos;
				sin = Math.sin;
				
				
				ra = ($rate - .25) * 360 * MathX.TO_RADIAN;
				px = cos(ra) * $radius;
				py = sin(ra) * $yradius;
				drawTo($x + px, $y + py);
				
				t0 = 1 - $rate;
				c = Math.ceil(Math.abs(MathX.decimalCal(t0, "/", 8)));
				r = t0 * 360 / c * Math.PI;
				c = - c;
				
				r2 = $radius * $radius;
				yr2 = $yradius * $yradius;
				drawTo = $graphics.curveTo;
				
				while ( c ++) {
					ra += r;
					x = cos(ra) * $radius;
					y = sin(ra) * $yradius;
					
					t0 = py * x;
					t1 = px * y;
					
					drawTo($x + r2 * (py - y) / (t0 - t1), $y + yr2 * (px - x) / (t1 - t0), $x + x, $y + y);
					
					px = x;
					py = y;
				}
			}
			else {
				$graphics.drawEllipse($x - $radius, $y - $yradius, $radius * 2, $yradius * 2);
			}
		}
	}

}